using System;
using System.Collections.Generic;
using System.Text;

namespace com.savage.mmtoxbmcexporter
{
    class RARUtils
    {
        private string m_exePath;
        private bool m_cleanupRarExe = false;
        private string m_archive = null;
        private List<string> m_filesToAdd = new List<string>();


        public RARUtils(string exePath)
        {
            m_exePath = exePath;
            m_cleanupRarExe = false;
        }

        public RARUtils()
        {
            m_exePath = ExtractRARExe();
            m_cleanupRarExe = true;
        }

        ~RARUtils()
        {
            if (m_cleanupRarExe)
            {
                System.IO.File.Delete(m_exePath);
            }
        }

        private string ExtractRARExe()
        {
            // extract pdftk.exe from assembly
            System.Reflection.Assembly assy = System.Reflection.Assembly.GetExecutingAssembly();
            System.IO.Stream rarexeStream;
            rarexeStream = assy.GetManifestResourceStream(this.GetType().Namespace + ".Rar.exe");

            // create temporary file
            string tempFileName = System.IO.Path.GetTempFileName();
            System.IO.FileStream tempFileStream = new System.IO.FileStream(tempFileName, System.IO.FileMode.Create);
            System.IO.BinaryWriter tempFile = new System.IO.BinaryWriter(tempFileStream);

            // write rarexeStream (containing rar.exe) to temp file
            int data = rarexeStream.ReadByte();
            while (data != -1)
            {
                byte byteData = Convert.ToByte(data);
                tempFile.Write((byte)byteData);
                data = rarexeStream.ReadByte();
            }
            tempFile.Close();
            return tempFileName;
        }



        public string ArchivePath
        {
            get
            {
                return m_archive;
            }
            set
            {
                m_archive = value;
            }
        }

        public List<string> FilesToAdd
        {
            get
            {
                return m_filesToAdd;
            }
        }

        public void Execute()
        {
            if (m_archive == null)
            {
                throw new Exception("Please set a valid archive name");
            }
            if (m_filesToAdd.Count == 0)
            {
                throw new Exception("Please add some files");
            }
            string args = string.Format("a {0} \"{1}\"", OptionsString, m_archive);
            foreach (string file in m_filesToAdd)
            {
                args += " \"" + file + "\"";
            }

            System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(m_exePath, args);
            psi.UseShellExecute = false;
            System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);
            Console.WriteLine(args);
            proc.WaitForExit();

        }

        private string OptionsString
        {
            get
            {
                // exclude paths from names
                // compression level: store
                // overwrite existing files
                // volume size = 1000000k (~1GB)
                // use old volume name scheme (r01/r02..../rar)
                return "-ep -m0 -o+ -v1000000k -vn";
            }
        }



    }
}
